package DAO;

import java.util.ArrayList;

import DTO.paintDTO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class paintDAO {
	public static final String KEY_MAGHICHU = "maghichu";
    public static final String KEY_URLPAINT = "urlPaint";
    public static final String KEY_ROWID = "_id";
    
    private static DatabaseHelper mDbHelper;
    private static  SQLiteDatabase mDb;
    private static final String TABLE_PAINT = "paint";
    

    public paintDAO() {
    }

    public static void open(Context ctx) throws SQLException {
        mDbHelper = new DatabaseHelper(ctx);
        mDb = mDbHelper.getWritableDatabase();
    }

    public static void close() {
        mDbHelper.close();
    }
    
    public static long createPaint(long mapaint,long maghichu, String url) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_ROWID, mapaint);
        initialValues.put(KEY_MAGHICHU, maghichu);
        initialValues.put(KEY_URLPAINT, url);
        Log.i("Paint","Them paint thanh cong");
        return mDb.insert(TABLE_PAINT, null, initialValues);
    }
    public static boolean deletePaint(long rowId) {
        return mDb.delete(TABLE_PAINT, KEY_ROWID + "=" + rowId, null) > 0;
    }
    
    public static boolean XoaTatCaPaintCuaMotGhiChu(long rowId) {
        return mDb.delete(TABLE_PAINT, KEY_MAGHICHU + "=" + rowId, null) > 0;
    }

    public static ArrayList<paintDTO> getAllPaint() {

    	Cursor cursor = mDb.query(TABLE_PAINT, new String[] {KEY_ROWID, KEY_MAGHICHU, KEY_URLPAINT}, 
    			null, null, null, null, null);
    	ArrayList<paintDTO> picArr = new ArrayList<paintDTO>();
        
        if (cursor != null ) {
            if  (cursor.moveToFirst()) {
                do {
                	paintDTO temp = new paintDTO();
                    temp.setUrlPaint(cursor.getString(cursor.getColumnIndex(paintDAO.KEY_URLPAINT)));
                    temp.setMaghichu(cursor.getLong(cursor.getColumnIndex(paintDAO.KEY_MAGHICHU)));
                    temp.setMaPic(cursor.getInt(cursor.getColumnIndex(paintDAO.KEY_ROWID)));
                    picArr.add(temp);
                }while (cursor.moveToNext());
            }
        }
        
        return picArr;
    }
    
    public static ArrayList<paintDTO> getAllPaintfromNote(long rowId) throws SQLException {
    	
        Cursor cursor = mDb.query(TABLE_PAINT, new String[] {KEY_ROWID, 
    			KEY_MAGHICHU, KEY_URLPAINT},KEY_MAGHICHU + "=" + rowId
    			,null, null, null, null, null);
        ArrayList<paintDTO> picArr = new ArrayList<paintDTO>();
        
        
        if (cursor != null ) {
            if  (cursor.moveToFirst()) {
                do {
                	paintDTO temp = new paintDTO();
                    temp.setUrlPaint(cursor.getString(cursor.getColumnIndex(paintDAO.KEY_URLPAINT)));
                    temp.setMaghichu(cursor.getLong(cursor.getColumnIndex(paintDAO.KEY_MAGHICHU)));
                    temp.setMaPic(cursor.getInt(cursor.getColumnIndex(paintDAO.KEY_ROWID)));
                    picArr.add(temp);
                }while (cursor.moveToNext());
            }
        }
        
        return picArr;

    }
    public static paintDTO getPaint(long rowId) throws SQLException {

        Cursor mCursor =
            mDb.query(true, TABLE_PAINT, new String[] {KEY_ROWID,
            		KEY_MAGHICHU,KEY_URLPAINT}, KEY_ROWID + "=" + rowId, null,
                    null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        paintDTO temp = new paintDTO();
        temp.setUrlPaint(mCursor.getString(mCursor.getColumnIndex(paintDAO.KEY_URLPAINT)));
        temp.setMaghichu(mCursor.getLong(mCursor.getColumnIndex(paintDAO.KEY_MAGHICHU)));
        temp.setMaPic(mCursor.getInt(mCursor.getColumnIndex(paintDAO.KEY_ROWID)));
        
        return temp;

    }
    public static boolean updatePaint(long rowId, long maghichu, String url) {
        ContentValues args = new ContentValues();
        args.put(KEY_MAGHICHU, maghichu);
        args.put(KEY_URLPAINT, url);

        return mDb.update(TABLE_PAINT, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
}
